import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

import 'package:get/get.dart';
import 'package:xiaomi_shop/app/modules/user/controllers/user_controller.dart';
import 'package:xiaomi_shop/app/services/screenAdapter.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/app/widget/logoWidget.dart';
import 'package:xiaomi_shop/models/messageModel.dart';

import '../controllers/phone_one_click_login_controller.dart';

class PhoneOneClickLoginView extends GetView<PhoneOneClickLoginController> {
  const PhoneOneClickLoginView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: IconButton(
          onPressed: () {
            Get.back();
          },
          icon: const Icon(Icons.close_outlined),
        ),
        actions: [
          TextButton(
              onPressed: () {},
              child: const Text(
                "帮助",
                style: TextStyle(color: Colors.black),
              ))
        ],
      ),
      body: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: Image.asset(
              "assets/images/logo_top_bg.png",
              fit: BoxFit.fill,
            ),
          ),
          ListView(
            children: [
              // logo
              LogoWidget(),
              // 手机号
              Container(
                margin: EdgeInsets.only(top: ScreenAdapter.height(80)),
                alignment: Alignment.center,
                child: Column(
                  children: [
                    Text(
                      controller.phoneNumber.replaceRange(3, 7, "****"),
                      style: TextStyle(
                          fontSize: ScreenAdapter.fontSize(80),
                          fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: ScreenAdapter.height(20),
                    ),
                    const Text(
                      "中国移动为您提供本机号码认证服务",
                      style: TextStyle(color: Colors.black54),
                    )
                  ],
                ),
              ),
              // 隐私政策
              Container(
                margin: EdgeInsets.only(top: ScreenAdapter.height(80)),
                padding: EdgeInsets.all(ScreenAdapter.width(30)),
                alignment: Alignment.center,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Obx(() => Checkbox(
                        shape: const CircleBorder(
                            side: BorderSide(color: Colors.black12)),
                        activeColor: Colors.red,
                        value: controller.agreePrivacy.value,
                        onChanged: (selected) {
                          controller.agreePrivacy.value =
                              !controller.agreePrivacy.value;
                        })),
                    Expanded(
                        child: RichText(
                      maxLines: 2,
                      text: TextSpan(
                          text: "使用手机号码登录并同意",
                          style: TextStyle(color: Colors.black54),
                          children: [
                            TextSpan(
                                text: "《京东隐私政策》",
                                style: TextStyle(color: Colors.blue.shade900),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () {
                                    print("《京东隐私政策》");
                                  }),
                            TextSpan(
                                text: "《中国移动认证服务条款》",
                                style: TextStyle(color: Colors.blue.shade900),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () {
                                    print("《中国移动认证服务条款》");
                                  })
                          ]),
                    )),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                    top: ScreenAdapter.height(80),
                    left: ScreenAdapter.height(80),
                    right: ScreenAdapter.height(80)),
                height: ScreenAdapter.height(136),
                child: ElevatedButton(
                    style: ButtonStyle(
                        backgroundColor: MaterialStatePropertyAll(Colors.red),
                        foregroundColor: MaterialStatePropertyAll(Colors.white),
                        textStyle: MaterialStatePropertyAll(TextStyle(
                            fontSize: ScreenAdapter.fontSize(40),
                            fontWeight: FontWeight.bold))),
                    onPressed: () async {
                      print("本机号码一键登录");
                      if (!controller.agreePrivacy.value) {
                        Toast.show("请阅读并同意服务条款");
                        return;
                      }
                      // 这里就使用账号密码登录模仿一键登录
                      MessageModel messageModel = await controller.login();
                      if (messageModel.success) {
                        Toast.show(messageModel.message);
                        // 更新用户信息
                        UserController userController =
                            Get.find<UserController>();
                        userController.getUserInfo();
                        Get.back();
                      } else {
                        Toast.show(messageModel.message);
                      }
                    },
                    child: const Text("本机号码一键登录")),
              ),
              Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(top: ScreenAdapter.height(400)),
                  child: SizedBox(
                    height: ScreenAdapter.height(100),
                    width: ScreenAdapter.width(600),
                    child: ElevatedButton(
                        style: ButtonStyle(
                            backgroundColor:
                                MaterialStatePropertyAll(Colors.grey.shade200),
                            foregroundColor:
                                MaterialStatePropertyAll(Colors.grey),
                            textStyle: MaterialStatePropertyAll(TextStyle(
                                fontSize: ScreenAdapter.fontSize(30),
                                fontWeight: FontWeight.normal)),
                            elevation: MaterialStatePropertyAll(0)),
                        onPressed: () {
                          Get.offAndToNamed("/phone-sms-login-step1");
                        },
                        child: const Text("其他方式登录")),
                  ))
            ],
          )
        ],
      ),
    );
  }
}
